<?php
namespace Ucmaim\Controller\Admin;

class MemberController extends CommonController{

    public function _initialize(){
        parent::_initialize();
    }

    public function Index(){
        $this->Lists();
    }

    public function Lists(){
        $Member = D('Member');
        $list = $Member->get();
        $list = gotoPage($list, $this->pageSize);
        $this->assign('list', $list['data']);
        $this->assign('page', $list['page']);
        $this->display('Lists');
    }


    public function Add(){
        $Member = D('Member');
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            $uid = $post['id'];
            //检查用户名
            $e = $Member->isExist('member_name', $post['member_name'], $uid);
            if( $e ) $this->error('此用户名已被占用');
            //密码
            if( empty($post['member_password']) ) $this->error('密码不能为空');
            //检查手机号码
            if( empty($post['member_mobile']) ) $this->error('手机号码不能为空');
            $e = $Member->isExist('member_mobile', $post['member_mobile'], $uid);
            if( $e ) $this->error('手机号码已被占用');
            //检查邮箱
            $e = $Member->isExist('member_email', $post['member_email'], $uid);
            if( $e ) $this->error('此邮箱已被占用');

            $data['member_name']   = $post['member_name'];
            $data['member_password'] = md5($post['member_password']);
            $data['member_mobile'] = $post['member_mobile'];
            $data['member_email']  = $post['member_email'];
            $data['member_qq']     = $post['member_qq'];
            $data['member_status'] = $post['member_status'];
            $data['member_time']   = time();
            $Member->addUser($data) ? $this->success('添加用户成功', U('Admin/Member/Index')) : $this->error('操作失败', U('Admin/Member/Index'));

        }else{
            $this->assign('action', '添加用户');
            $this->display('User');
        }
    }

    // 编辑用户信息
    public function Edit(){
        $Member = D('Member');
        if( IS_POST ){
            $post = I('post.', '', 'trim');
            $uid = $post['id'];
            //检查用户名
            $e = $Member->isExist('member_name', $post['member_name'], $uid);
            if( $e ) $this->error('此用户名已被占用');
            //检查手机号码
            $e = $Member->isExist('member_mobile', $post['member_mobile'], $uid);
            if( $e ) $this->error('手机号码已被占用');
            //检查邮箱
            $e = $Member->isExist('member_email', $post['member_email'], $uid);
            if( $e ) $this->error('此邮箱已被占用');
            $data['member_name']   = $post['member_name'];
            $data['member_mobile'] = $post['member_mobile'];
            $data['member_email']  = $post['member_email'];
            $data['member_qq']     = $post['member_qq'];
            $data['member_status'] = $post['member_status'];
            if( $post['member_password'] !== '' ){
                $data['member_password'] = md5($post['member_password']);
            }
            $Member->updateUser($uid, $data) ? $this->success('操作成功', U('Admin/Member/Index')) : $this->error('操作失败', U('Admin/Member/Index'));
        }else{
            $uid = (int)I('get.uid');
            $info = $Member->memberInfo($uid);
            $this->assign('member', $info);
            $this->assign('action', '修改用户信息');
            $this->display('User');
        }
    }

    // 禁用用户
    public function Lock(){
        $Member = D('Member');
        $uid = (int)I('get.uid');
        $Member->updateMemberStatus($uid, 0) ? $this->error('用户已禁用', U('Admin/Member/Index')) : $this->error('操作失败', U('Admin/Member/Index'));
    }

    // 激活用户
    public function Unlock(){
        $Member = D('Member');
        $uid = (int)I('get.uid');
        $Member->updateMemberStatus($uid, 1) ? $this->error('用户已激活', U('Admin/Member/Index')) : $this->error('操作失败', U('Admin/Member/Index'));
    }

    //搜索用户
    public function Search(){
        $keyword = I('get.keyword');
        $M = M('Member');
        if(!empty($keyword)) {
            $where['member_name|member_mobile|member_email'] = $keyword;
            $M = $M->where($where);
        }
        $this->list = $M->select();
        $this->assign('keyword', $keyword);
        $this->display('Lists');
    }
}